<?php
namespace Home\Controller;
use Think\Controller;

/**
 * web
 * 聊天模块
 */
class ChatController extends Controller {
	public function index(){
		layout(false);
		$this->display();
	}
	public function test(){
		layout(false);
		$this->display();
	}
	public function server(){
		layout(false);
		$host = 'localhost'; //host
		$port = '8080'; //port
		$null = NULL; //null var

		//Create TCP/IP sream socket
		$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		//reuseable port
		socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

		//bind socket to specified host
		socket_bind($socket, 0, $port);

		//listen to port
		socket_listen($socket);

		//create & add listning socket to the list
		$clients = array($socket);
	}
	public function login(){
		$username = trim($_POST['username']);
		$save = array();
		$save['user_name'] = $username;
		$save['create_at'] = date("Y-m-d H:i:s");
		
		if(M('online')->add($save)){
			session('K-user_name',$username);
			cookie('K-user_name',$username);
			echo 1;
		}else{
			echo 0;
		}
	}
	public function loginout(){
		session('K-user_name',null); 
		cookie('K-user_name',null);
	}
}